<?php

namespace Platform\Repository\Organize;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Platform\Models\Organize\LeaderOperationLog;
use Platform\Models\Organize\Organize;
use Platform\Repository\Repository;

class LogRepository extends Repository
{

    public function model()
    {
        return  LeaderOperationLog::class;
    }

    /**
     * 通过关键词查询
     * @param Request $request
     * @return Builder
     */
    public function screen(Request $request,Organize $organize){
        $builder = $this->model->newQuery();
        $builder->where('organize_id', '=', $organize->id);

        $method = $request->get('method',null);
        $ip = $request->get('ip',null);
        $name = $request->get('name',null);
        $begin_at = $request->get('begin_at',null);
        $end_at = $request->get('end_at',null);

        $builder->with('leader');

        if ($method){
            $builder->where('method', '=', $method);
        }
        if ($ip){
            $builder->where('ip', '=', $ip);
        }
        if ($begin_at){
            $builder->whereDate('created_at', '>=', $begin_at);
        }
        if ($end_at){
            $builder->whereDate('created_at', '<=', $end_at);
        }
        if ($name){
            $builder->whereHas('leader', function ($query) use ($name){
                $query->where('name', 'like', '%'.$name.'%');
            });
        }

        return $builder;
    }


}
